package com.lz.ovuola.service.assembler;

import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lz.ovuola.entity.PhysiologicalBasisDo;
import com.lz.ovuola.entity.UserDo;
import com.lz.ovuola.service.dto.LoginUserDto;
import com.lz.ovuola.util.exception.BusinessException;
import com.lz.ovuola.util.exception.msg.CommonMsgEnum;
import com.lz.ovuola.util.threadlocal.UserContext;
import com.lz.ovuola.util.threadlocal.UserContextHolder;

/**
 * 包装工具
 * 
 * @author fz
 *
 */
public class LoginUserAssembler implements Assembler<LoginUserDto> {

	// private final LoginUserDto EMPTY_DATA = new LoginUserDto();
	private Logger logger = LoggerFactory.getLogger("LoginUserAssembler");
	private static final LoginUserAssembler ASSEMBLER = new LoginUserAssembler();

	private LoginUserAssembler() {
		super();
	}

	@Override
	public LoginUserDto toDto(Object object) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public LoginUserDto toDto(UserContext userContext, UserDo userDo,
			PhysiologicalBasisDo physiologicalBasisDo) throws Exception {
		if (ObjectUtils.notEqual(userContext, null)) {
			LoginUserDto loginUserDto = new LoginUserDto();
			loginUserDto.setId(userDo.getId());
			loginUserDto.setNickName(userDo.getNickName());
			loginUserDto.setHeadImage(userDo.getHeadImage());
			loginUserDto.setHardwareEquipment(userDo.getHardwareEquipment());
			loginUserDto.setBindingTime(userDo.getBindingTime());
			loginUserDto.setUser(userContext.getUser());
			loginUserDto.setToken(userContext.getToken());
			loginUserDto.setExpiration(userContext.getExpiration());
			if (ObjectUtils.notEqual(physiologicalBasisDo, null)) {
				loginUserDto.setCycleFloatingDays(physiologicalBasisDo
						.getCycleFloatingDays());
				loginUserDto.setIsUpload(1);
				loginUserDto.setPhysiologicalLength(physiologicalBasisDo
						.getPhysiologicalLength());
				loginUserDto.setMenstruationLength(physiologicalBasisDo
						.getMenstruationLength());
			} else {
				loginUserDto.setIsUpload(2);
			}
			return loginUserDto;

		} else {
			logger.error("---用户id：{},转loginUserDto过程中出现异常", UserContextHolder
					.get().getUserId());// UserContextHolder
			throw new BusinessException(
					CommonMsgEnum.SERVER_INTERNAL_ERROR.getStatus(),
					CommonMsgEnum.SERVER_INTERNAL_ERROR.getMsg());

		}

	}

	public static LoginUserAssembler getAssembler() {
		return ASSEMBLER;
	}

}
